package com.sc.test.recyclerview2;

import android.graphics.Canvas;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sc.test.R;
import com.sc.test.RecyclerBaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Shen Chao on 2018-03-13-013 上午 9:21.
 */

public class SwipeToDismissActivity extends RecyclerBaseActivity {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private SwipeToDismissAdapter mAdapter;
    private ItemTouchHelper mItemTouchHelper;
    private ItemTouchHelper.Callback mCallback;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_swipe);
        ButterKnife.bind(this);
        initAction();
    }

    private void initAction() {
        mCallback = new ItemTouchHelper.Callback() {
            /**
             * 设置滑动类型标记
             * @param recyclerView
             * @param viewHolder
             * @return 返回整型标识，用于判断那种滑动是有效的
             */
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END);
            }

            /**
             * 是否支持滑动
             * @return
             */
            @Override
            public boolean isItemViewSwipeEnabled() {
                return true;
            }

            /**
             * 是否支持长按
             * @return
             */
            @Override
            public boolean isLongPressDragEnabled() {
                return false;
            }

            /**
             * 拖拽切换item回调
             * @param recyclerView
             * @param viewHolder
             * @param target
             * @return 如果item换了位置，返回true
             */
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return true;
            }

            /**
             * 滑动删除item
             * @param viewHolder
             * @param direction
             */
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                mAdapter.delete(viewHolder.getAdapterPosition());
            }

            /**
             * 被选中的回调
             * @param viewHolder
             * @param actionState
             *          当前Item的状态
             *          ItemTouchHelper.ACTION_STATE_IDLE   闲置状态
             *          ItemTouchHelper.ACTION_STATE_SWIPE  滑动中状态
             *          ItemTouchHelper#ACTION_STATE_DRAG   拖拽中状态
             */
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                //item被选中的操作
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                    viewHolder.itemView.setBackgroundResource(R.color.gray);
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            /**
             * 移动过程中绘制item
             * @param c
             * @param recyclerView
             * @param viewHolder
             * @param dX    x轴移动的距离
             * @param dY    y轴移动的距离
             * @param actionState   当前item状态
             * @param isCurrentlyActive     如果当前被用户操作为true
             */
            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                float x = Math.abs(dX);
                float width = viewHolder.itemView.getWidth();
                float alpha = 1f - x / width;
                viewHolder.itemView.setAlpha(alpha);
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }

            /**
             * 操作完毕后调用
             * @param recyclerView
             * @param viewHolder
             */
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                //操作完恢复颜色
                viewHolder.itemView.setBackgroundResource(R.color.white);
                viewHolder.itemView.setAlpha(1.0f);
                super.clearView(recyclerView, viewHolder);
            }
        };

        mItemTouchHelper = new ItemTouchHelper(mCallback);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new SwipeToDismissAdapter(InitData.getData());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        mItemTouchHelper.attachToRecyclerView(recyclerView);
    }
}
